package com.delivery.service.impl;

import com.delivery.context.BaseContext;
import com.delivery.dto.ShoppingCartAddDTO;
import com.delivery.entity.Dish;
import com.delivery.entity.Setmeal;
import com.delivery.entity.ShoppingCart;
import com.delivery.exception.BaseException;
import com.delivery.mapper.DishMapper;
import com.delivery.mapper.SetmealMapper;
import com.delivery.mapper.ShoppingCartMapper;
import com.delivery.service.ShoppingCartService;
import com.delivery.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper seatmealMapper;
    /**
     * 添加购物车
     * @param dto
     */
    @Override
    public void addCart(ShoppingCartAddDTO dto) {

        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(dto, shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());
        // 先看看购物车里面有没有
        ShoppingCart cart = shoppingCartMapper.selectBy(shoppingCart);

        if (cart == null) {
            // 购物车中没有
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            if  (dto.getDishId() != null) {
                // 添加的是菜品
                // 查询菜品
                Dish dish = dishMapper.selectById(dto.getDishId());
                shoppingCart.setAmount(dish.getPrice());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setName(dish.getName());

                shoppingCartMapper.insert(shoppingCart);


            }else if (dto.getSetmealId() != null){
                // 添加的是套餐
                // 查询套餐
                SetmealVO setmealVO = seatmealMapper.selectById(dto.getSetmealId());
                shoppingCart.setAmount(setmealVO.getPrice());
                shoppingCart.setImage(setmealVO.getImage());
                shoppingCart.setName(setmealVO.getName());

                shoppingCartMapper.insert(shoppingCart);
            }
        }else {
            // 购物车有
            cart.setNumber(cart.getNumber() + 1);
            shoppingCartMapper.updateNumber(cart);
        }


    }

    /**
     * 查看购物车
     * @return
     */
    @Override
    public List<ShoppingCart> viewCart() {

        List<ShoppingCart> carts = shoppingCartMapper.selectByUserId(BaseContext.getCurrentId());
        return carts;
    }

    @Override
    public void deleteCart(ShoppingCartAddDTO dto) {

        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(dto, shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());
        // 先看看购物车里面有没有
        ShoppingCart cart = shoppingCartMapper.selectBy(shoppingCart);

        if (cart == null) {
            // 购物车中没有
            throw new BaseException("购物车中没有该商品");
        }else {
            // 购物车有
            if (cart.getNumber() == 1) {
                shoppingCartMapper.deleteById(cart.getId());
            }else{
                cart.setNumber(cart.getNumber() - 1);
                shoppingCartMapper.updateNumber(cart);
            }

        }

    }

    @Override
    public void cleanCart() {

        shoppingCartMapper.deleteByUserId(BaseContext.getCurrentId());
    }
}
